#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
import tqdm


def load_json(path: str):
    import json
    with open(path) as fp:
        return json.load(fp)


def download(adcode: str, name: str):
    url = 'https://geo.datav.aliyun.com/areas_v2/bound/{}_full.json'.format(
        adcode)
    resp = requests.get(url)
    if resp.status_code != 200:
        print('下载 {}({}) 的数据失败'.format(name, adcode))
        return
    with open('{}.json'.format(adcode), 'wb') as fp:
        fp.write(resp.content)


def main():
    config = load_json('index.json')
    progress = tqdm.tqdm(total=len(config))
    for k, v in config:
        download(k, v)
        progress.update(1)
    progress.close()


if __name__ == '__main__':
    main()
